package scriptrunner.execution;

import java.io.Serializable;

public class ValueFloatObject extends ValueObject implements Serializable,
		Cloneable, ArithmeticCalculable, ComparableValue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2985529118632105867L;

	public ValueFloatObject() {
		this.value = 0;
	}

	public ValueFloatObject(float v) {
		this.value = v;
	}

	@Override
	public ValueFloatObject clone() throws CloneNotSupportedException {
		ValueFloatObject o = (ValueFloatObject) super.clone();
		o.value = value;
		return o;
	}

	public void SetValue(float v) {
		value = v;
	}

	public float GetValue() {
		return value;
	}

	float value;

	@Override
	public ValueFloatObject Add(Addable o) throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject result = new ValueFloatObject();
		result.value = value + ((ValueFloatObject) o).value;
		return result;
	}

	@Override
	public ValueFloatObject Minus(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject result = new ValueFloatObject();
		result.value = value - ((ValueFloatObject) o).value;
		return result;
	}

	@Override
	public ValueFloatObject Times(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject result = new ValueFloatObject();
		result.value = value * ((ValueFloatObject) o).value;
		return result;
	}

	@Override
	public ValueFloatObject Divide(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject result = new ValueFloatObject();
		result.value = value / ((ValueFloatObject) o).value;
		return result;
	}

	@Override
	public ValueFloatObject UnaryMinus() throws ExecuteErrorException {
		ValueFloatObject result = new ValueFloatObject();
		result.value = -value;
		return result;
	}

	@Override
	public void ParseFromString(String s) throws ExecuteErrorException {
		try {
			value = Float.parseFloat(s);
		} catch (Throwable e) {
			throw (new ExecuteErrorException("Cannot convert the value to an Float"));
		}
	}

	@Override
	public int ArithmeticTypePreference() {
		return 1;
	}

	@Override
	public String SaveValueToString() {
		return Float.toString(value);
	}

	@Override
	public int AddTypePerference() {
		return 1;
	}

	@Override
	public boolean LessThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value < rhsValue.value;
	}

	@Override
	public boolean GreaterThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value > rhsValue.value;
	}

	@Override
	public boolean LessEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value <= rhsValue.value;
	}

	@Override
	public boolean GreaterEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value >= rhsValue.value;
	}

	@Override
	public boolean EqualTo(EqualAndNotEqual rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value == rhsValue.value;
	}

	@Override
	public boolean NotEqualTo(EqualAndNotEqual rhs)
			throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueFloatObject rhsValue = (ValueFloatObject) rhs;

		return value != rhsValue.value;
	}
	
	@Override
	public boolean equals(Object o) {
		if ( o == null || o.getClass() != getClass())
			return false;
		ValueFloatObject rhs = (ValueFloatObject)o;
		return value == rhs.value;
	}
	
	@Override
	public String toString() {
		return Float.toString(value);
	}
}
